package cn.wjh.business.controller;

import cn.wjh.business.domain.Works;
import cn.wjh.business.service.IWorksService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 比赛作品Controller
 *
 * @author jiraWu
 * @date 2023-10-20
 */
@RestController
@RequestMapping("/business/works")
public class WorksController extends BaseController {
	@Resource
	private IWorksService worksService;

	/**
	 * 查询比赛作品列表
	 * @param works
	 * @return
	 */
//	@PreAuthorize("@ss.hasPermi('business:works:list')")
	@GetMapping("/list")
	@Anonymous
	public TableDataInfo list(Works works) {
		startPage();
		List<Works> list = worksService.selectWorksList(works);
		return getDataTable(list);
	}
	@GetMapping("/my")
	public TableDataInfo myList(Works works) {
		startPage();
		List<Works> list = worksService.myWorks(works);
		return getDataTable(list);
	}

	/**
	 * 导出比赛作品列表
	 */
//	@PreAuthorize("@ss.hasPermi('business:works:export')")
	@Log(title = "比赛作品", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, Works works) {
		List<Works> list = worksService.selectWorksList(works);
		ExcelUtil<Works> util = new ExcelUtil<Works>(Works.class);
		util.exportExcel(response, list, "比赛作品数据");
	}

	/**
	 * 获取比赛作品详细信息
	 */
//	@PreAuthorize("@ss.hasPermi('business:works:query')")
	@GetMapping(value = "/{worksId}")
	@Anonymous
	public AjaxResult getInfo(@PathVariable("worksId") Long worksId) {
		return success(worksService.selectWorksByWorksId(worksId));
	}

	/**
	 * 新增比赛作品
	 */
//	@PreAuthorize("@ss.hasPermi('business:works:add')")
	@Log(title = "比赛作品", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody Works works) {
		return toAjax(worksService.insertWorks(works));
	}

	/**
	 * 修改比赛作品
	 */
//	@PreAuthorize("@ss.hasPermi('business:works:edit')")
	@Log(title = "比赛作品", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody Works works) {
		return toAjax(worksService.updateWorks(works));
	}

	/**
	 * 删除比赛作品
	 */
//	@PreAuthorize("@ss.hasPermi('business:works:remove')")
	@Log(title = "比赛作品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{worksIds}")
	public AjaxResult remove(@PathVariable Long[] worksIds) {
		return toAjax(worksService.deleteWorksByWorksIds(worksIds));
	}

	/*获取比赛作品提交状态*/
//	@PreAuthorize("@ss.hasPermi('business:works:isPublish')")
	@GetMapping("/status/{competitionId}")
	public AjaxResult isPublish(@PathVariable Long competitionId) {
		AjaxResult result = success();
		boolean status = worksService.isPublish(competitionId);
		result.put(AjaxResult.DATA_TAG, status);
		return result;
	}

	/*获取我的作品详细根据用户Id和比赛Id*/
	@GetMapping("/byCompetitionId/{competitionId}")
	public AjaxResult getInfoByCompetitionIdAndUserId(@PathVariable Long competitionId) {
		return success(worksService.getInfoByCompetitionIdAndUserId(SecurityUtils.getUserId(),competitionId));
	}

	/**
	 * 获取作品排名数据
	 * @param works
	 * @return
	 */
	@GetMapping("/rank")
	@Anonymous
	public TableDataInfo rank(Works works) {
		startPage();
		List<Works> list = worksService.rank(works);
		return getDataTable(list);
	}

	/**
	 * 判断作品点评状态
	 * @param
	 * @return
	 */
	@GetMapping("/judgement")
	public TableDataInfo judgement(Works works) {
		startPage();
		List<Works> list = worksService.judgement(works);
		return getDataTable(list);
	}
}
